package utils

import (
	"bytes"
	"io/ioutil"
	"net/http"
)

type req struct {}

// 请求
func Req() *req{
	return &req{}
}

// Get 发送Get请求
func (r *req) Get(url string) string {
	resp, err := http.Get(url)
	if err == nil {
		defer resp.Body.Close()

		body, err := ioutil.ReadAll(resp.Body)
		if err == nil {
			return string(body)
		}
	}
	return ""
}

// Post 发送Post请求
func (r *req) Post(url, data string) []byte {
	resp, err := http.Post(url, "application/json", bytes.NewBuffer([]byte(data)))
	if err == nil {
		defer resp.Body.Close()

		body, err := ioutil.ReadAll(resp.Body)
		if err == nil {
			return body
		}
	}
	return nil
}